﻿using System;
using Microsoft.SharePoint;

namespace arb.SharePoint.SecureSilverlight
{
    public class FeatureReceiver :SPFeatureReceiver
    {
        #region Overriden Abstract Inherited Members
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            var site = properties.Feature.Parent as SPSite;
            if (site == null)
            {
                return;
            }
            SPWeb web = site.RootWeb;
            SPUser user = web.CurrentUser;
            SPList list;

            if (web.ListExists("WebPart Instances"))
            {
                list = web.Lists["WebPart Instances"];
            }
            else
            {
                Guid id = web.Lists.Add("WebPart Instances", "Holder For WebPart Instantiation", SPListTemplateType.GenericList);
                list = web.Lists[id];
            }

            list.RemoveContentType(SPBuiltInContentTypeId.Item);
            list.AddContentType();
            list.BreakRoleInheritance(false);
            list.Update();

            SPView view = list.DefaultView;
            view.ViewFields.Add(Constants.PageTitleField);
            view.ViewFields.Add(Constants.PageUrlField);
            view.ViewFields.Add(Constants.SilverlightFileField);
            view.ViewFields.Add(Constants.LastModifiedDateField);
            view.Update();

            
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            var site = properties.Feature.Parent as SPSite;
            if (site == null)
            {
                return;
            }
            SPList list = null;
            using (SPWeb web = site.RootWeb)
            {
                if (web.ListExists("WebPart Instances"))
                {
                    list = web.Lists["WebPart Instances"];
                }
            }
            if (list != null)
            {
                list.Delete();
            }
        } 
        #endregion

        #region Not Overriden
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        } 
        #endregion
    }
}
